package cate.game.server.service;

import cate.game.GameBody;
import cate.game.event.master.RoleLoginEvent;
import cate.game.robot.RobotEditor;
import cate.game.role.Role;
import cate.game.util.api.LoginApi;
import cp.solution.business.robot.RobotUtils;
import cp.solution.business.robot.cmd.RobotCreateResult;
import cp.solution.business.robot.record.RobotRecord;
import cp.solution.business.sys.account.AccountEntity;
import cp.solution.business.sys.account.dto.AccountLoginData;
import cp.solution.define.SexType;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class RobotService {

    @Resource
    UsefulCtx uc;

    public RobotCreateResult create(int srvId, int mode, String token) {
        RobotCreateResult result = new RobotCreateResult();
        result.setSrvId(srvId);
        GameBody game = uc.getGame(srvId);
        AccountLoginData ld = game.apis().login.account.getLoginData(token);
        if(ld == null) {
            return result.fail("AccountLoginData找不到");
        }
        game.stat.whenAccountLogin(ld.getAccount());

        String uid = ObjectId.get().toHexString();
        Role role = new Role(game, uid);
        AccountEntity account = ld.getAccount();
        role.getBase().accountId = account.getId();
        role.getBase().read(ld);
        role.getBase().name = "机器"+ RobotUtils.randomName();
        role.getBase().sex = Math.random() < 0.5 ? SexType.MALE.value : SexType.FEMALE.value;
        role.getBase().langCode = "zh-CN";
        role.getBase().srvIdOrigin = game.config.srvId;
        role.getBase().visitorId = "机器人没有的";
        role.getStatus().createTime = System.currentTimeMillis();
        role.onCreate();

        game.role.putActive(role);

        RobotEditor.edit(role, game, mode);

        try {
            LoginApi.regToLogin(game, role);
        } catch (Exception e) {
            log.error("", e);
            return result.fail("LoginApi.regToLogin失败!" + e.getMessage());
        }

        game.stat.gameHandler.addRoleNum();
        role.newAllPart();
        game.eventDispatcher.dispatchEvent(new RoleLoginEvent(role));
        game.stat.whenLogin(role);

        RobotRecord robotRec = new RobotRecord();
        robotRec.setRoleId(role.getUid());
        robotRec.setRoleName(role.getBase().name);
        robotRec.setSrvId(srvId);
        robotRec.setUsername(ld.getAccount().getUsername());
        result.setRobotRec(robotRec);
        return result;
    }

    public int delete(int srvId, String roleId) {
        GameBody game = uc.getGame(srvId);
        game.role.removeFromMemory(roleId);
        game.facilities.getMongo().roleBase.deleteById(roleId);
        game.facilities.getMongo().roleBag.deleteById(roleId);
        game.facilities.getMongo().roleFight.deleteById(roleId);
        game.facilities.getMongo().roleMail.deleteById(roleId);
        game.facilities.getMongo().roleFriend.deleteById(roleId);
        game.facilities.getMongo().roleMall.deleteById(roleId);
        game.facilities.getMongo().roleChat.deleteById(roleId);
        game.facilities.getMongo().rolePay.deleteById(roleId);
        game.facilities.getMongo().roleWorld.deleteById(roleId);
        game.facilities.getMongo().roleNote.deleteById(roleId);
        game.facilities.getMongo().roleSystem.deleteById(roleId);
        game.facilities.getMongo().mailBox.deleteById(roleId);
        game.facilities.getMongo().friendBox.deleteById(roleId);
        game.facilities.getMongo().status.deleteById(roleId);
        game.facilities.getMongo().res.deleteById(roleId);
        game.facilities.getMongo().farm.deleteById(roleId);
        game.facilities.getMongo().pvp.deleteById(roleId);
        game.facilities.getMongo().activity.deleteById(roleId);
        game.facilities.getMongo().task.deleteById(roleId);
        game.facilities.getMongo().history.deleteById(roleId);
        game.facilities.getMongo().show.deleteById(roleId);
        return 1;
    }

}
